# coding=utf-8
from sqlalchemy import BigInteger, Column, String, Boolean
from sqlalchemy.orm import relationship
from uzwan import cache
from uzwan import db

class Channel(db.Model):
    '''
    推广渠道信息
    '''
    __tablename__ = 'channels'

    id = Column('id', BigInteger, primary_key=True)
    code = Column('code', String(255)) # 渠道编码
    name = Column('name', String(255)) # 渠道名称
    self = Column('self', Boolean) # 是否自由渠道

    accounts = relationship('Account', cascade='save-update', backref='channel')
    guests = relationship('AccountGuest', cascade='save-update', backref='channel')

    @staticmethod
    @cache.cached('model:channel', timeout=7200)
    def get_self_channel():
        return db.session.query(Channel).filter(Channel.self == True).first()

    @staticmethod
    @cache.cached('model:channel')
    def get_by_code(code):
        return db.session.query(Channel).filter(Channel.code == code).first()

